#!/usr/bin/python3
# -*- encoding: utf-8 -*-
'''
@File        :urllib请求.py
@Time        :2020/09/06 22:51:52
@Author      :He
@Software    :vsCode
'''

from urllib.request import urlopen, Request
import random
from urllib.parse import urlencode

user_agents = [
    "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
    "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
    "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4238.0 Safari/537.36"
]

headers = {
    'User-Agent': random.choice(user_agents)
}

args = {
    'wd': '知乎'
}
url = "https://www.baidu.com/s?{}".format(urlencode(args))
request = Request(url, headers=headers)
# 发送请求
resp = urlopen(request)
# 请求状态
if resp.getcode() == 200:
    # 读取内容    decode()默认UTF-8
    info = resp.read().decode()
    print(info)

    # # 返回实际访问的url
    # print(resp.geturl())
